package com.qef.com.mmovie.mvp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVObject;
import com.qef.com.mmovie.App;
import com.qef.com.mmovie.R;
import com.qef.com.mmovie.base.BaseRecycerViewAdapter;
import com.qef.com.mmovie.entity.DataBean;
import com.qef.com.mmovie.entity.VVIdeoDetailsEntity;
import com.qef.com.mmovie.img.ImageLoader;
import com.qef.com.mmovie.utils.Constants;
import com.qef.com.mmovie.utils.DateUtil;
import com.qef.com.mmovie.utils.GsonUtils;
import com.qef.com.mmovie.utils.SystemUtil;

import java.util.List;

/**
 * 收藏页面adapter
 */
public class CollectionAdapter extends BaseRecycerViewAdapter<AVObject, CollectionAdapter.Myholder> {
    public CollectionAdapter(Context context, List<AVObject> list) {
        super(context, list);
    }

    @Override
    public Myholder getCreateViewHolder(ViewGroup parent, int viewType) {
        return new Myholder(View.inflate(App.getInstance(), R.layout.item_collection, null));
    }

    @Override
    public void getBindViewHolder(Myholder holder, int position) {
        if ((int) list.get(position).get("video_type") == 1) {
            DataBean dataBean = (DataBean) GsonUtils.
                    jsonToBean(list.get(position).get(Constants.Collection.VIDEO_INFO).toString(), DataBean.class);
            ImageLoader.getInstance().displayImage(App.getInstance(), dataBean.getCover().getDetail(),
                    holder.ivVideo, SystemUtil.dp2px(200), SystemUtil.dp2px(110));
            holder.tvTitle.setText(dataBean.getTitle());
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("#").append(dataBean.getCategory())
                    .append(" ")
                    .append(" / ")
                    .append(" ")
                    .append(DateUtil.formatTime2(dataBean.getDuration()));
            holder.tvDescription.setText(stringBuilder.toString());
            holder.itemView.setOnClickListener(v -> {
                if (mClickListener != null) {
                    mClickListener.onItemClick(position, v, holder);
                }
            });
            holder.itemView.setOnLongClickListener(view -> {
                mOnItemLongClickListener.onItemLongClick(position, view, holder);
                return true;
            });
        } else {
            VVIdeoDetailsEntity dataBean = (VVIdeoDetailsEntity) GsonUtils.
                    jsonToBean(list.get(position).get(Constants.Collection.VIDEO_INFO).toString(), VVIdeoDetailsEntity.class);
            ImageLoader.getInstance().displayImage(App.getInstance(), dataBean.getData().getImage(),
                    holder.ivVideo, SystemUtil.dp2px(200), SystemUtil.dp2px(110));
            holder.tvTitle.setText(dataBean.getData().getTitle());
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("#").append(dataBean.getData().getCate())
                    .append(" ")
                    .append(" / ")
                    .append(" ")
                    .append(DateUtil.formatTime2(Integer.parseInt(dataBean.getData().getContent().getVideo().get(0).getDuration())));
            holder.tvDescription.setText(stringBuilder.toString());
            holder.itemView.setOnClickListener(v -> {
                if (mClickListener != null) {
                    mClickListener.onItemClick(position, v, holder);
                }
            });
            holder.itemView.setOnLongClickListener(view -> {
                mOnItemLongClickListener.onItemLongClick(position, view, holder);
                return true;
            });

        }
    }

    class Myholder extends RecyclerView.ViewHolder {
        ImageView ivVideo;
        TextView tvTitle;
        TextView tvDescription;

        public Myholder(View itemView) {
            super(itemView);
            ivVideo = (ImageView) itemView.findViewById(R.id.iv_video);
            tvTitle = (TextView) itemView.findViewById(R.id.title);
            tvDescription = (TextView) itemView.findViewById(R.id.description);
        }
    }
}
